/**
 * 🌍 登录页面国际化文本键管理
 *
 * 遵循 i18n 键值命名规范：
 * - 层级结构：页面.组件.功能.具体文本
 * - 命名风格：camelCase
 * - 可扩展性：预留多语言支持结构
 */

export interface LoginLocale {
  // 页面标题和副标题
  title: string
  subtitle: string

  // 表单相关
  form: {
    username: {
      placeholder: string
      label: string
      error: {
        required: string
        invalid: string
      }
    }
    password: {
      placeholder: string
      label: string
      toggle: {
        show: string
        hide: string
      }
      error: {
        required: string
        minLength: string
        invalid: string
      }
    }
    submit: {
      text: string
      loading: string
    }
  }

  // 测试账号信息
  testAccount: {
    title: string
    username: string
    password: string
    securityNotice: string
    actions: {
      copy: string
      fill: string
      copied: string
      copyFailed: string
    }
  }

  // 状态和反馈消息
  messages: {
    success: {
      login: string
      copy: string
    }
    error: {
      login: string
      network: string
      validation: string
      unauthorized: string
      serverError: string
      copy: string
    }
    warning: {
      defaultPassword: string
    }
  }

  // 无障碍访问
  accessibility: {
    loginButton: string
    usernameField: string
      passwordField: string
    passwordToggle: string
    testAccountInfo: string
    copyButton: string
    loadingIndicator: string
  }

  // 元数据
  meta: {
    description: string
    keywords: string[]
  }
}

// 默认中文语言包
export const zhCN: LoginLocale = {
  title: '溃疡性结肠炎智能评分系统',
  subtitle: 'EndoSight-UC 医生工作站',

  form: {
    username: {
      placeholder: '请输入工号',
      label: '工号',
      error: {
        required: '请输入工号',
        invalid: '工号格式不正确'
      }
    },
    password: {
      placeholder: '请输入密码',
      label: '密码',
      toggle: {
        show: '显示密码',
        hide: '隐藏密码'
      },
      error: {
        required: '请输入密码',
        minLength: '密码长度不能少于6位',
        invalid: '密码格式不正确'
      }
    },
    submit: {
      text: '登录',
      loading: '登录中...'
    }
  },

  testAccount: {
    title: '测试账号信息',
    username: 'admin',
    password: 'admin123',
    securityNotice: '点击账号信息可快速填充 · 安全提示：请在生产环境中修改默认密码',
    actions: {
      copy: '复制',
      fill: '填充',
      copied: '已复制到剪贴板',
      copyFailed: '复制失败'
    }
  },

  messages: {
    success: {
      login: '登录成功',
      copy: '已复制到剪贴板'
    },
    error: {
      login: '登录失败',
      network: '网络连接失败，请检查服务器是否运行',
      validation: '表单验证失败，请检查输入内容',
      unauthorized: '工号或密码错误',
      serverError: '服务器内部错误',
      copy: '复制失败'
    },
    warning: {
      defaultPassword: '您正在使用默认密码，建议修改以确保账户安全'
    }
  },

  accessibility: {
    loginButton: '登录按钮，提交登录表单',
    usernameField: '工号输入框，请输入您的工号',
    passwordField: '密码输入框，请输入您的密码',
    passwordToggle: '密码显示切换按钮',
    testAccountInfo: '测试账号信息区域',
    copyButton: '复制按钮，复制账号信息到剪贴板',
    loadingIndicator: '加载指示器，正在处理登录请求'
  },

  meta: {
    description: '溃疡性结肠炎智能评分系统登录页面 - 医生工作站入口',
    keywords: [
      '溃疡性结肠炎',
      'UCEIS',
      '智能评分',
      '医疗系统',
      '医生工作站',
      '登录',
      'EndoSight-UC'
    ]
  }
}

// 英文语言包（预留）
export const enUS: LoginLocale = {
  title: 'Ulcerative Colitis Intelligent Scoring System',
  subtitle: 'EndoSight-UC Medical Workstation',

  form: {
    username: {
      placeholder: 'Enter employee ID',
      label: 'Employee ID',
      error: {
        required: 'Please enter employee ID',
        invalid: 'Invalid employee ID format'
      }
    },
    password: {
      placeholder: 'Enter password',
      label: 'Password',
      toggle: {
        show: 'Show password',
        hide: 'Hide password'
      },
      error: {
        required: 'Please enter password',
        minLength: 'Password must be at least 6 characters',
        invalid: 'Invalid password format'
      }
    },
    submit: {
      text: 'Login',
      loading: 'Logging in...'
    }
  },

  testAccount: {
    title: 'Test Account Information',
    username: 'admin',
    password: 'admin123',
    securityNotice: 'Click account info to auto-fill · Security: Please change default password in production',
    actions: {
      copy: 'Copy',
      fill: 'Fill',
      copied: 'Copied to clipboard',
      copyFailed: 'Copy failed'
    }
  },

  messages: {
    success: {
      login: 'Login successful',
      copy: 'Copied to clipboard'
    },
    error: {
      login: 'Login failed',
      network: 'Network connection failed, please check if server is running',
      validation: 'Form validation failed, please check your input',
      unauthorized: 'Invalid employee ID or password',
      serverError: 'Internal server error',
      copy: 'Copy failed'
    },
    warning: {
      defaultPassword: 'You are using the default password, please change it to ensure account security'
    }
  },

  accessibility: {
    loginButton: 'Login button, submit login form',
    usernameField: 'Employee ID input field, please enter your employee ID',
    passwordField: 'Password input field, please enter your password',
    passwordToggle: 'Password visibility toggle button',
    testAccountInfo: 'Test account information area',
    copyButton: 'Copy button, copy account info to clipboard',
    loadingIndicator: 'Loading indicator, processing login request'
  },

  meta: {
    description: 'Ulcerative Colitis Intelligent Scoring System Login Page - Medical Workstation Entrance',
    keywords: [
      'Ulcerative Colitis',
      'UCEIS',
      'Intelligent Scoring',
      'Medical System',
      'Medical Workstation',
      'Login',
      'EndoSight-UC'
    ]
  }
}

// 语言包映射
export const loginLocales = {
  'zh-CN': zhCN,
  'en-US': enUS
} as const

// 当前语言类型
export type LoginLocaleKey = keyof typeof loginLocales

// 默认语言
export const defaultLoginLocale: LoginLocaleKey = 'zh-CN'

// 工具函数：获取嵌套对象的值
export function getNestedValue<T extends Record<string, any>>(
  obj: T,
  path: string
): string | undefined {
  return path.split('.').reduce((current, key) => {
    return current?.[key]
  }, obj) as string | undefined
}

// 工具函数：设置嵌套对象的值
export function setNestedValue<T extends Record<string, any>>(
  obj: T,
  path: string,
  value: string
): void {
  const keys = path.split('.')
  const lastKey = keys.pop()!

  const target = keys.reduce((current, key) => {
    if (!current[key]) {
      current[key] = {}
    }
    return current[key]
  }, obj)

  target[lastKey] = value
}

// 获取当前语言的文本
export function getLoginText(
  locale: LoginLocaleKey = defaultLoginLocale,
  key?: string
): LoginLocale | string {
  const localeData = loginLocales[locale]

  if (!key) {
    return localeData
  }

  return getNestedValue(localeData, key) || key
}

// 便捷的文本获取函数（用于组件中）
export function t(key: string, locale?: LoginLocaleKey): string {
  return getLoginText(locale, key) as string
}